﻿using UnityEngine;
using System.Collections;

public class ManagedSpawner : MonoBehaviour {

	public GameObject dragonShip;
	public GameObject raptorShip;
	public GameObject motherShip;
	public GameObject spawnPortal;
	public float MissionStartWait;
	public float SpawnWait;
	public bool allSpawned;

	GameObject spawnClone;
	Vector3 portalPosition;
	int difficulty;
	int enemyOnes;
	int enemyTwos;
	int enemyThrees;
	int totalEnemies;
	int[] thisWave = new int[1000];
	int dragonDiff = 1;
	int raptorDiff = 5;
	int motherDiff = 10;

	// Use this for initialization
	void Start ()
	{
	}
	
	// Update is called once per frame
	void Update ()
	{
	}

	public void SelectEnemies(int MissionDifficulty)
	{
		// Initialize Values
		int currentDifficulty = 0;
		difficulty = MissionDifficulty;
		enemyOnes = 0;
		enemyTwos = 0;
		enemyThrees = 0;

		// Use mission difficulty
		// Randomly select an enemy difficulty
		// Count up this difficulty until it reaches mission difficulty
		for (int e = 0; e < difficulty; e++)
		{
			if (currentDifficulty < difficulty)
			{
				int tempEnemy = Random.Range(0, 3);
				
				if (tempEnemy == 0)
				{
					if ((currentDifficulty + dragonDiff) <= difficulty)
					{
						enemyOnes++;
						currentDifficulty = currentDifficulty + dragonDiff;
					}
				}
				else if (tempEnemy == 1)
				{
					if ((currentDifficulty + raptorDiff) <= difficulty)
					{
						enemyTwos++;
						currentDifficulty = currentDifficulty + raptorDiff;
					}
					else
					{
						enemyOnes++;
						currentDifficulty = currentDifficulty + dragonDiff;
					}
				}
				else if (tempEnemy == 2)
				{
					if ((currentDifficulty + motherDiff) <= difficulty)
					{
						enemyThrees++;
						currentDifficulty = currentDifficulty + motherDiff;
					}
					else if ((currentDifficulty + raptorDiff) <= difficulty)
					{
						enemyTwos++;
						currentDifficulty = currentDifficulty + raptorDiff;
					}
					else
					{
						enemyOnes++;
						currentDifficulty = currentDifficulty + dragonDiff;
					}
				}
			}
		}
	}

	public void SetupWave()
	{
		// Find total number of enemies
		totalEnemies = enemyOnes + enemyTwos + enemyThrees;
		Debug.Log("Total Enemies " + totalEnemies);
		Debug.Log ("Dragonflies: " + enemyOnes);
		Debug.Log ("Winged Raptors: " + enemyTwos);
		Debug.Log ("Motherships: " + enemyThrees);

		// Use temporary values to manipulate for wave selection
		int tempOne = enemyOnes;
		int tempTwo = enemyTwos;
		int tempThree = enemyThrees;

		// Select enemies to put into each wave
		for (int e = 0; e < totalEnemies; e++)
		{
			// Place Dragonfly into wave.
			if (tempOne > 0)
			{
				for (int a = 0; a < thisWave.Length; a++)
				{
					if (thisWave[a] == 0)
					{ thisWave[a] = 1; tempOne--; break;}
				}
			}

			// Place Winged Raptor into wave.
			if (tempTwo > 0)
			{
				for (int a = 0; a < thisWave.Length; a++)
				{
					if (thisWave[a] == 0)
					{ thisWave[a] = 2; tempTwo--; break;}
				}
			}

			// Place Mothership into wave.
			if (tempThree > 0)
			{
				for (int a = 0; a < thisWave.Length; a++)
				{
					if (thisWave[a] == 0)
					{ thisWave[a] = 3; tempThree--; break;}
				}
			}
		}
	}

	void SpawnPortal()
	{
		// Spawn Portal for enemies to come from.
		portalPosition = new Vector3(Random.Range (-10.0f, 10.0f), Random.Range (-10.0f, 10.0f), -8);
		spawnClone = (GameObject)Instantiate (spawnPortal, portalPosition, spawnPortal.transform.rotation);
		Debug.Log("Spawned Portal for Mission");
	}

	public void SpawnMission()
	{
		allSpawned = false;
		SpawnPortal ();
		StartCoroutine (SpawnEnemies ());
		Debug.Log ("Started to Spawn Enemies for Current Mission");
	}

	IEnumerator SpawnEnemies()
	{
		yield return new WaitForSeconds (MissionStartWait);
		while(true)
		{
			// Get length of wave based on existing enemies within array.
			int tempWaveLength = 0;
			for (int a = 0; a < thisWave.Length; a++)
			{
				if (thisWave[a] == 0)
				{ tempWaveLength = a; Debug.Log("Length of Wave: " + tempWaveLength); break;}
			}
			
			// Spawn an enemy based on type.
			for (int i = 0; i <= tempWaveLength; i++)
			{
				Vector3 spawnPosition = new Vector3 (portalPosition.x, portalPosition.y, -9);
				Quaternion spawnRotation = Quaternion.identity;
				
				if (thisWave[i] == 0)
				{ break; }
				else if (thisWave[i] == 1)
				{
					//enemy_Type = "Dragonfly";
					Instantiate (dragonShip, spawnPosition, dragonShip.transform.rotation);
				}
				else if (thisWave[i] == 2)
				{
					//enemy_Type = "Winged Raptor";
					Instantiate (raptorShip, spawnPosition, raptorShip.transform.rotation);
				}
				else if (thisWave[i] == 3)
				{
					//enemy_Type = "Mothership";
					Instantiate (motherShip, spawnPosition, motherShip.transform.rotation);
				}
				yield return new WaitForSeconds(SpawnWait);
				Debug.Log ("Spawned a " + thisWave[i]);
			}
			// All enemies spawned, destroy the spawning portal.
			Destroy(spawnClone);
			Debug.Log ("Destroyed current Spawn Portal.");
			allSpawned = true;
			Debug.Log ("Spawned entire wave");
			break;
		}
	}
	



}
